//
//  ShopSettingsViewController.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/23.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "ShopSettingsViewController.h"
#import "ShopSettingTableViewCell.h"
#import "MoneyViewController.h"
#import "CouponViewController.h"
#import "ShopTimeViewController.h"
#import "ApplyforShopViewController.h"
#import "MyAlbumViewController.h"
#import "ApplyforShopViewController.h"
#import "NSNotiViewController.h"
#import "ZJImageMagnification.h"
#import <JMessage/JMessage.h>
#import "ChatRoomViewController.h"
#import "CommitViewController.h"

@interface ShopSettingsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UIImageView * topView;
@property (nonatomic, weak)UIButton * leftButton;
@property (nonatomic, weak)UIButton * rightButton;
@property (nonatomic, strong)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, strong)UIImageView * loccalImageView;
@property (nonatomic, weak)UILabel * localLabel;
@property (nonatomic, strong)NSDictionary * dict;

@property (nonatomic, strong)NSArray * titleArray;
@property (nonatomic, strong)NSArray * imgArray;

@property (nonatomic, weak)UIToolbar *toolbar;
@property (nonatomic, weak)UIView * helpView;

@end

@implementation ShopSettingsViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self loadData];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)loadData{
    
    NSString * shop_id = DEF_GET_OBJECT(@"shopid");
    
    [[ZRNetworkTool sharedTool] getMerchantsInfoWithShop_id:shop_id WithBlock:^(BOOL success, id responseData) {

        if (success) {
            
            self.dict = responseData[@"data"];
            self.nameLabel.text = self.dict[@"merchants_name"];
            self.localLabel.text = self.dict[@"merchants_address"];
            NSString * img = self.dict[@"merchants_img"];
            NSArray * albumArray = [img componentsSeparatedByString:@","];
            if([albumArray.firstObject containsString:@"http"]) {
                
                NSURL * url = [NSURL URLWithString:albumArray.firstObject];
                [self.iconImageView sd_setImageWithURL:url];
            } else {
                
                NSString * imgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,albumArray.firstObject];
                NSURL * url = [NSURL URLWithString:imgStr];
                
                [self.iconImageView sd_setImageWithURL:url];
            }

           
        }
    }];
    
    NSString * ID = DEF_GET_OBJECT(@"id");
    
    [[ZRNetworkTool sharedTool] judgeUserNoticeWithUser_id:ID ResultWithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.rightButton.selected = YES;
        }else{
            
            self.rightButton.selected = NO;
        }
    }];
}


- (void)setDict:(NSDictionary *)dict{
    
    _dict = dict;
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ColorWithRGB(0xEFF0F1);
    self.navigationItem.title = @"";
    //@"余额"
    self.titleArray = @[@"营业中",@"优惠券",@"店铺信息",@"本店相册"];
    self.imgArray = @[@"营业中",@"优惠券",@"店铺信息",@"相册"];
    
    [self setupUI];
}


- (void)setupUI {
    
    UIImageView * topView = [UIImageView new];
    
//    topView.backgroundColor = ColorWithRGB(0xFAE15A);
    NSUInteger r = arc4random_uniform(4) + 1;
    NSString * imgName = [NSString stringWithFormat:@"背景%lu",(unsigned long)r];
    topView.image = [UIImage imageNamed:imgName];
    topView.userInteractionEnabled = YES;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    
    UIButton * leftButton = [UIButton new];

    [leftButton setImage:[UIImage imageNamed:@"客服2"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(clickSeverButton:) forControlEvents:UIControlEventTouchUpInside];

    self.leftButton = leftButton;

    [self.topView addSubview:leftButton];
    
    //系统消息
    UIButton * rightButton = [UIButton new];
    
    [rightButton setImage:[UIImage imageNamed:@"系统消息2"] forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:@"系统消息1"] forState:UIControlStateSelected];
    [rightButton addTarget:self action:@selector(clickSeverButton:) forControlEvents:UIControlEventTouchUpInside];
    rightButton.selected = NO;
    
    self.rightButton = rightButton;
    
    [self.view addSubview:rightButton];
    
    
    self.iconImageView = [self setupImageViewWithImage:@"组12"];
    self.iconImageView.layer.cornerRadius = 45 * SCREENH6;
    self.iconImageView.layer.masksToBounds = YES;
    self.iconImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGesture)];
    [self.iconImageView addGestureRecognizer:tap];
    
    [self.topView addSubview:self.iconImageView];
    
    self.loccalImageView = [self setupImageViewWithImage:@"placeholder-1"];
    [self.topView addSubview:self.loccalImageView];
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.text = @"店铺名";
    nameLabel.textColor = ZRWhiteColor;
    nameLabel.font = [UIFont systemFontOfSize:20 * SCREENH6];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    
    self.nameLabel = nameLabel;
    
    [self.topView addSubview:self.nameLabel];
    
    
    UILabel * localLabel = [UILabel new];
    
    localLabel.text = @"成都金牛大道168号";
    localLabel.textColor = ZRWhiteColor;
    localLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    localLabel.textAlignment = NSTextAlignmentCenter;
    
    self.localLabel = localLabel;
    
    [self.topView addSubview:self.localLabel];
//    int tabHeight = (iPhoneX ? 44.f : 20.f);220
//    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(15 * SCREENH6, kStatusBarHeight + 156 * SCREENH6 + kNavHeight, 345 * SCREENH6, 204 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ZRWhiteColor;//ColorWithRGB(0xEFF0F1);
    tableView.rowHeight = 51 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.layer.cornerRadius = 5;
    tableView.layer.masksToBounds = YES;
    tableView.scrollEnabled = NO;

    self.tableView = tableView;
    
    [self.tableView registerClass:[ShopSettingTableViewCell class] forCellReuseIdentifier:@"ShopSettingTableViewCell"];
    
    [self.view addSubview:self.tableView];
}


- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(300 * SCREENH6);
    }];
    
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(kStatusBarHeight + 7 * SCREENH6);
        make.width.mas_equalTo(22 * SCREENH6);
        make.height.mas_equalTo(19 * SCREENH6);
    }];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(kStatusBarHeight + 7 * SCREENH6);
        make.width.mas_equalTo(23 * SCREENH6);
        make.height.mas_equalTo(23 * SCREENH6);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.rightButton.mas_bottom);
        make.height.width.mas_equalTo(90 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(10 * SCREENH6);
        make.width.mas_equalTo(345 * SCREENH6);
    }];
    
    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(2 * SCREENH6);
//        make.width.mas_equalTo(300 * SCREENH6);
    }];
    
    [self.loccalImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.localLabel.mas_leading).offset(-2 * SCREENH6);
        make.centerY.mas_equalTo(self.localLabel);
        make.width.mas_equalTo(11 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.titleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShopSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShopSettingTableViewCell" forIndexPath:indexPath];
    
    cell.iconImageView.image = [UIImage imageNamed:self.imgArray[indexPath.row]];
    cell.titleLabel.text = self.titleArray[indexPath.row];
    if (indexPath.row == 1) {
        
        if (self.dict) {
            
            cell.subLabel.text = [NSString stringWithFormat:@"%@",self.dict[@"merchants_coupons_number"]];
        }
        cell.billSwitch.hidden = YES;
    }else if (indexPath.row == 0){
        
        if (self.dict) {
            
            int status = [self.dict[@"merchants_status"] intValue];
            if (status == 1) {
                //营业中
                cell.titleLabel.text = @"营业中";
                cell.billSwitch.on = YES;
            }else if (status == 0){
                //休息中
                cell.titleLabel.text = @"休息中";
                cell.billSwitch.on = NO;
            }
            
        }
        
        cell.subLabel.hidden = YES;
        cell.rightImageView.hidden = YES;
        cell.billSwitch.hidden = NO;
    }else if (indexPath.row == 2){
        
        cell.subLabel.hidden = YES;
        cell.rightImageView.hidden = NO;
        cell.billSwitch.hidden = YES;
    }else if (indexPath.row == 3){
        
        if (self.dict) {
            
            NSString * img = [NSString stringWithFormat:@"%@",self.dict[@"merchants_photo_album_number"]];
            
            cell.subLabel.text = [NSString stringWithFormat:@"%@张",img];
        }
        
        cell.billSwitch.hidden = YES;
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    
    if (indexPath.row == 0) {
        
        //        ShopSettingTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        
//        ShopTimeViewController * timeVC = [ShopTimeViewController new];
        
        //        [timeVC returnText:^(NSString *showText) {
        //
        //            cell.subLabel.text = showText;
        //        }];
        
//        [self.navigationController pushViewController:timeVC animated:YES];
        
    }else if (indexPath.row == 1){
        
        CouponViewController * couponVC = [CouponViewController new];
        
        [self.navigationController pushViewController:couponVC animated:YES];
        
    }else if (indexPath.row == 2){
        
        ApplyforShopViewController * shopVC = [ApplyforShopViewController new];
        
        shopVC.dict = self.dict;
        
        [self.navigationController pushViewController:shopVC animated:YES];
        
    }else if (indexPath.row == 3){
        
        MyAlbumViewController * albumVC = [MyAlbumViewController new];
        
        albumVC.type = @"2";
        
        [self.navigationController pushViewController:albumVC animated:YES];
        
    }
    
    
    ShowBar;
}


- (UIImageView *)setupImageViewWithImage:(NSString *)img{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:img];
    
    return imageView;
}


- (void)clickSeverButton:(UIButton *)button{
    
    if (self.leftButton == button) {
        
        [self clickSeverceButton];
    }else{
        
        HiddenBar;
        
        NSNotiViewController * notiVC = [NSNotiViewController new];
        
        [self.navigationController pushViewController:notiVC animated:YES];
        
        ShowBar;
    }
}

- (void)singleTapGesture{
    
    [ZJImageMagnification scanBigImageWithImageView:self.iconImageView alpha:1];
}


- (void)clickSeverceButton{
    
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //样式
    toolbar.barStyle = UIBarStyleBlackTranslucent;//半透明
    //透明度
    toolbar.alpha = 0.5f;
    toolbar.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [toolbar addGestureRecognizer:tapGR];
    
    self.toolbar = toolbar;
    [self.tabBarController.view addSubview:self.toolbar];
    
    UIView * helpView = [[UIView alloc] initWithFrame:CGRectMake(62.5 * SCREENH6, kScreenH/2 - 120 * SCREENH6, 250 * SCREENH6, 203 * SCREENH6)];
    
    helpView.backgroundColor = ZRWhiteColor;
    helpView.userInteractionEnabled = YES;
    helpView.layer.cornerRadius = 5 * SCREENH6;
    helpView.layer.masksToBounds = YES;
    
    self.helpView = helpView;
    
    [self.tabBarController.view addSubview:self.helpView];
    
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 8 * SCREENH6, 250 * SCREENH6, 30 * SCREENH6)];
    
    titleLabel.text = @"联系客服";
    titleLabel.textColor = ZRBlackColor;
    titleLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.helpView addSubview:titleLabel];
    
    
    UIButton * msgButton = [[UIButton alloc] init];
    
    msgButton.tag = 2001;
    [msgButton setImage:[UIImage imageNamed:@"在线客服"] forState:UIControlStateNormal];
    [msgButton addTarget:self action:@selector(clickCallOrMsgToSeverice:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.helpView addSubview:msgButton];
    
    UIButton * telButton = [[UIButton alloc] initWithFrame:CGRectMake(10 * SCREENH6, 96 * SCREENH6, 230 * SCREENH6, 30 * SCREENH6)];
    
    telButton.tag = 2002;
    [telButton setImage:[UIImage imageNamed:@"客服电话"] forState:UIControlStateNormal];
    [telButton addTarget:self action:@selector(clickCallOrMsgToSeverice:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.helpView addSubview:telButton];
    
    UIButton * commitButton = [[UIButton alloc] init];
    
    commitButton.tag = 2003;
    [commitButton setImage:[UIImage imageNamed:@"常见问题"] forState:UIControlStateNormal];
    [commitButton addTarget:self action:@selector(clickCallOrMsgToSeverice:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.helpView addSubview:commitButton];
    
    
    UIButton * closeButton = [[UIButton alloc] initWithFrame:CGRectMake(50 * SCREENH6, 146 * SCREENH6, 150 * SCREENH6, 30 * SCREENH6)];
    
    closeButton.tag = 2004;
    [closeButton setImage:[UIImage imageNamed:@"关闭(1)"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(clickCallOrMsgToSeverice:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.helpView addSubview:closeButton];
    
    //    [helpView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerX.mas_equalTo(self.view);
    //        make.centerY.mas_equalTo(self.view).offset(20 * SCREENH6);
    //        make.width.mas_equalTo(250 * SCREENH6);
    //        make.height.mas_equalTo(203 * SCREENH6);
    //    }];
    //
    //    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerX.mas_equalTo(helpView);
    //        make.top.mas_equalTo(helpView).offset(15 * SCREENH6);
    //    }];
    
    [commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.helpView);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(15 * SCREENH6);
        make.width.mas_equalTo(200 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
    
    [msgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(commitButton);
        make.top.mas_equalTo(commitButton.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [telButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(msgButton);
        make.top.mas_equalTo(msgButton.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(titleLabel);
        make.trailing.mas_equalTo(self.helpView).offset(-10 * SCREENH6);
        make.width.height.mas_equalTo(25 * SCREENH6);
        //        make.height.mas_equalTo(30 * SCREENH6);
    }];
}

- (void)tapAction{
    
    [self.toolbar removeFromSuperview];
    [self.helpView removeFromSuperview];
}
- (void)clickCallOrMsgToSeverice:(UIButton *)button{
    
    if (button.tag == 2001) {
        
        NSString * merchants_admin_phone = @"17781489591";
        
        [JMSGConversation createSingleConversationWithUsername:merchants_admin_phone completionHandler:^(id resultObject, NSError *error) {
            
            if (error == nil) {
                
                [self.toolbar removeFromSuperview];
                [self.helpView removeFromSuperview];
                
                HiddenBar;
                ChatRoomViewController * roomVC = [ChatRoomViewController new];
                
                roomVC.conversation = resultObject;
                
                [self.navigationController pushViewController:roomVC animated:YES];
                
                ShowBar;
                
            } else {
                
            }
        }];
        
    }else if (button.tag == 2002){
        
        NSString * str = @"tel:028-65279781";
        
        UIWebView * callWebview = [[UIWebView alloc] init];
        
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        
        [self.view addSubview:callWebview];
        
        [self.toolbar removeFromSuperview];
        [self.helpView removeFromSuperview];
        
    }else if (button.tag == 2003){
        
        [self.toolbar removeFromSuperview];
        [self.helpView removeFromSuperview];
        
        HiddenBar;
        CommitViewController * commitVC = [CommitViewController new];
        
        [self.navigationController pushViewController:commitVC animated:YES];
        
        ShowBar;
    }
    else if (button.tag == 2004){
        
        [self.toolbar removeFromSuperview];
        [self.helpView removeFromSuperview];
    }
}


@end
